﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using YouHP.Utility;

namespace YouHP.Model
{
    public class PatientSearchCondition
    {
        public string PatientName {get; set;}       //病人名称
        public string DiseaseName  {get; set;}      //疾病名称
        public int DiseaseId {get; set;}            //疾病id
        public string SymptomName { get; set; }     //症状名称
        public int SymptomId { get; set; }          //症状id
        public string TreatmentName { get; set; }   //治疗名称
        public int TreatmentId { get; set; }        //治疗id
        public int AgeStart { get; set; }           //起始年龄点
        public int AgeEnd { get; set; }             //结束年龄点
        public int ProvinceId { get; set; }         //省份id
        public int CityId { get; set; }             //城市id
        public string Gender { get; set; }          //性别

        public int StartDosage { get; set; }        //服用的计量
        public int EndDosage { get; set; }

        public int SymptomDegree { get; set; }

        public int IsQueryCurrent { get; set; }     // 是否查询当前表: 0 - 查询历史表, 1 - 查询当前表

        public int IsAdvanceSearch { get; set; }    //是否高级检索
        public int OrderByField { get; set; }       //排序字段

        public const int UPDATE_TIME = 1;           //按更新次数排序
        public const int VIEW_COUNT = 2;            //按被浏览次数排序
        public const int LAST_UPDATE_DATE = 3;      //按最近更新时间排序
        public const int REGISTERED_DATE = 4;       //按注册时间排序

        public PatientSearchCondition()
        {
            PatientName = DiseaseName = SymptomName = TreatmentName = Gender = string.Empty;
            DiseaseId = ProvinceId = CityId = AgeStart = AgeEnd = - 1;
            IsAdvanceSearch = 0;
            IsQueryCurrent = 1;
        }

        public string ToSqlConditonString()
        {
            string sql = string.Format(" [UserInfo].[UserTypeId] = {0} ", Constants.USER_PATIENT);

            /*if (DiseaseName.Length > 0)
            {
                sql += string.Format(" AND [Disease].[DiseaseName] like '%{0}%' ", DiseaseName);
            }*/

            if (DiseaseId > 0)
            {
                sql += string.Format(" AND [PatientRecord].[DiseaseId]={0} ", DiseaseId);
            }

            /*if (SymptomName.Length > 0)
            {
                sql += string.Format(" AND [Symptom].[SymptomName] like '%{0}%' ", SymptomName);
            }*/

            if (SymptomId > 0)
            {
                string tableName = string.Empty;
                if (IsQueryCurrent == 1)
                    tableName = "PatientCurrentSymptom";
                else
                    tableName = "PatientSymptomHistory";

                sql += string.Format(" AND [SymptomId]={0} ",  SymptomId);
                if (SymptomDegree > 0)
                {
                    sql += string.Format(" AND [DegreeValue]={0}",  SymptomDegree);
                }
                else
                {
                    sql += string.Format("AND [DegreeValue]>0");
                }
            }

            if (TreatmentId > 0)
            {
                string tableName;
                if (IsQueryCurrent == 1)
                    tableName = "PatientCurrentTreatment";
                else
                    tableName = "PatientTreatmentHistory";

                sql += string.Format(" AND [TreatmentId]={1}", tableName, TreatmentId);
                if( StartDosage >= 0 ) 
                {
                    sql += string.Format(" AND [DataValue]>={1}", tableName, StartDosage);
                }
                if(EndDosage > 0 )
                {
                    sql += string.Format(" AND [DataValue]<{1}", tableName, EndDosage);
                }

            }

            if ( TreatmentName != "")
            {
                sql += string.Format(" AND [Treatment].[Treatmentname] like '%{0}%'", TreatmentName.Trim());
            }

            if (ProvinceId > 0)
            {
                sql += string.Format(" AND [UserInfo].[ProvinceId] = {0} ", ProvinceId);
            }

            if (CityId > 0)
            {
                sql += string.Format(" AND [UserInfo].[CityId] = {0} ", CityId);
            }

            if (AgeStart > 0)
            {
                sql += string.Format(" AND DATEDIFF(year, [UserInfo].[Birthday], getdate()) >= {0} ", AgeStart);
            }

            if (AgeEnd > 0)
            {
                sql += string.Format(" AND DATEDIFF(year, [UserInfo].[Birthday], getdate()) <= {0} ", AgeEnd);
            }

            if(Gender.Length > 0)
            {
                sql += string.Format(" AND Gender = '{0}' ", Gender);
            }

            if (PatientName.Length > 0)
            {
                sql += string.Format(" AND [UserInfo].[UserName] like '%{0}%'  ", PatientName);
            }

            return sql;
        }

        public string ToOrderByString()
        {
            string orderStr = "";

            switch (OrderByField)
            {
                case UPDATE_TIME:
                    orderStr = " [PatientRecord].[UpdateTime] desc, [ViewCount] desc ";
                    break;
                case LAST_UPDATE_DATE:
                    orderStr = " [UserInfo].[LastUpdateDate] desc, [ViewCount] desc  ";
                    break;
                case REGISTERED_DATE:
                    orderStr = " [UserInfo].[RegisteredDate] desc, [ViewCount] desc  ";
                    break;
                case VIEW_COUNT:
                default:
                    orderStr = "[UpdateTime] desc"; 
                    break;
            }

            return orderStr;
        }
    }
}